#!/usr/bin/env python

import os, time, math
from random import random

from perseus import Perseus

from Crypto.Cipher import AES
import base64

PADDING = '{'
BLOCK_SIZE = 32

pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)

def Entropy(data) :
   entropy = 0
   for x in range(256) :
      p_x = float(data.count(chr(x)))/len(data)
      if p_x > 0 :
         entropy += - p_x*math.log(p_x, 2)
   return entropy


fd = open("../message_test.txt", "r")
TEST = fd.readlines()
fd.close()

entropy_cl = 0
for test in TEST :
   entropy_cl += Entropy( test )

secret = os.urandom(BLOCK_SIZE)
cipher = AES.new(secret)

p = Perseus(aleas=[ random() for i in range(0,4) ])

entropy = 0
t1 = time.time()
for test in TEST :
   encoded_msg = p.code( test )
   decoded_msg = p.decode( encoded_msg )
   entropy += Entropy( encoded_msg )
t2 = time.time()
print "PERSEUS", t2-t1, entropy_cl / len(TEST), entropy / len(TEST)

entropy = 0
t1 = time.time()
for test in TEST :
   encoded_msg = EncodeAES(cipher, test)
   decoded_msg = DecodeAES(cipher, encoded_msg)
   entropy += Entropy( encoded_msg )
t2 = time.time()
print "AES", t2-t1, entropy_cl / len(TEST), entropy / len(TEST)
